package com.aaa.dao.impl;

import com.aaa.dao.MovieDao;
import com.aaa.entity.Movie;
import com.aaa.entity.Mtype;
import com.aaa.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Controller
public class MovieDaoImpl implements MovieDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    private RowMapper<Movie> rowMapper=new RowMapper<Movie>() {
        public Movie mapRow(ResultSet resultSet, int i) throws SQLException {
            Movie movie=new Movie();
            movie.setId(resultSet.getLong("id"));
            movie.setName(resultSet.getString("name"));
            movie.setPicture(resultSet.getString("picture"));
            movie.setPlot(resultSet.getString("plot"));
            movie.setAuthor(resultSet.getString("author"));
            movie.setMtime(resultSet.getString("mtime"));
            movie.setPrice(resultSet.getString("price"));
            movie.setLead(resultSet.getString("lead"));
            movie.setLeadp(resultSet.getString("leadp"));
            Object mid = resultSet.getInt("mid");
            if(mid!=null){
                Mtype mtype=new Mtype();
                mtype.setMid(resultSet.getLong("mid"));
                mtype.setMname(resultSet.getString("mname"));
                movie.setMtype(mtype);
            }
            return movie;
        }
    };
    public List<Movie> listPage(String keyword, Integer offset, Integer limit) {
        String sql="select m.id,m.name,m.picture,m.plot,m.author,m.mtime,m.price,m.lead,m.leadp,t.mid,t.mname from movie m left join mtype t on m.mid=t.mid where m.name like ? order by m.id desc limit ?,?";
        List<Movie> result = this.jdbcTemplate.query(sql, rowMapper, "%" + keyword + "%", offset, limit);
        return result;
    }

    public Integer count(String keyword) {
        String sql = "select count(id) as c from movie where name like ? ";
        Integer result = this.jdbcTemplate.queryForObject(sql, new RowMapper<Integer>() {
            public Integer mapRow(ResultSet resultSet, int i) throws SQLException {
                return resultSet.getInt("c");
            }
        },"%"+keyword+"%");
        return  result;
    }

    public Integer save(Movie movie){
        String sql="insert into movie(name,picture,plot,author,mtime,price,lead,leadp,mid) values(?,?,?,?,?,?,?,?,?)";
        int update = jdbcTemplate.update(sql, movie.getName(),movie.getPicture(),movie.getPlot(),movie.getAuthor(),movie.getMtime(), movie.getPrice(),movie.getLead(),movie.getLeadp(),movie.getMtype().getMid());
        return update;
    }
    public Integer update(Movie movie){
        String sql="update movie set name=?,picture=?,plot=?,author=?,mtime=?,price=?,lead=?,leadp=?,mid=? where id=?";
        int update = jdbcTemplate.update(sql,movie.getName(),movie.getPicture(),movie.getPlot(),movie.getAuthor(),movie.getMtime(), movie.getPrice(),movie.getLead(),movie.getLeadp(),movie.getMtype().getMid(),movie.getId());
        return update;
    }
    public Integer delete(Long id){
        String sql="delete from movie where id=?";
        int update = jdbcTemplate.update(sql, id);
        return update;
    }
}
